<?php

class Email{

	private $row_id;
	private $email;
	private $confirmado;
	
	public function setRow_id($arg){
		$this->row_id = $arg;
	}

	public function getRow_id(){
		return $this->row_id;
	}

	public function setEmail($arg){
		$this->email = $arg;
	}

	public function getEmail(){
		return $this->email;
	}
	
	public function setConfirmado($arg){
		$this->confirmado = $arg;
	}

	public function getConfirmado(){
		return $this->confirmado;
	}

	public function listar($db){

		$sql = "SELECT
	              row_id, email, confirmado
            FROM
              emails
            WHERE
              confirmado = 1";

		$res = $db->executar($sql);
		
		while($reg = mysql_fetch_array($res)){
			
			$mail = new Email();

			$mail->setRow_id($reg["row_id"]);
			$mail->setEmail($reg["email"]);
			$mail->setConfirmado($reg["confirmado"]);
			
			$lista[] = $mail;
		}
		
		return $lista;
	}

	public function getObjEmail($db, $email){

		$sql = "SELECT
	              row_id, email, confirmado
            FROM
              emails
            WHERE
              email = '{$email}'";

		$res = $db->executar($sql);
		
		if($reg = mysql_fetch_array($res)){

			$this->setRow_id($reg["row_id"]);
			$this->setEmail($reg["email"]);
			$this->setConfirmado($reg["confirmado"]);
		}
		
		return $this;
	}

	public function dbInserir($db){

		$sql = "INSERT INTO emails 
            	VALUES (
	            null,
	            '{$this->getEmail()}', 0)";

		$db->executarEmail($sql);

		$id = mysql_insert_id();

		$db->desconectar();

		return $id;
	}
	
	public function dbConfirmarEmail($db, $row_id){

		$sql = "UPDATE emails 
            	set confirmado = 1 
			   WHERE row_id = {$row_id}";

		$db->executar($sql);
	}
}

?>
